package com.ymatou.smsproxy.api.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.smsproxy.api.parameter.SmsProxyBean;
import com.ymatou.smsproxy.api.service.SmsProxyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.common.enums.MessageSupplyEnum;
import com.ymttest.database.model.SmsLog;
import com.ymttest.database.sqlwapper.SmsLogWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

@TestSuite
public class Ts_SmsProxy2 {
	private static SmsProxyBean spb;
	private static SmsProxyCall spc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("短信网关通道分离-新增多种配置");
	}

	@Before
	public void caseUp() {
		spb = new SmsProxyBean();
		spc = new SmsProxyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(spc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SmsProxy2_001() {
		Logger.start(true,"默认配置发送验证码成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			String msg = "sms send message test";
			Logger.comment("手机号:" + phone);
			spb.setMsg(msg);
			spb.setPhone(phone);
			spc.setData(spb);
			spc.callService();
			Logger.comment("SmsProxyCall send:" + spc.getEntityString());
			Logger.comment("SmsProxyCall return:" + spc.getReturnData());
			Thread.sleep(8000);
			List<SmsLog> sms = new SmsLogWapper().selectByPhone(phone);
			Logger.verifyEquals(true, sms.size() == 4, "验证发送短信");
			if (sms.size() > 0) {
				for (SmsLog s : sms) {
					Logger.debug(s.getAction());
					if (!s.getAction().equals("Received")) {
						Logger.verifyEquals(MessageSupplyEnum.WM梦网平台.toString(), s.getMessagesupply(), "验证发送的渠道");
						Logger.verifyEquals(true, s.getMessage().contains(msg), "验证短信内容");
					}
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SmsProxy2_002() {
		Logger.start(true,"新建配置发送验证码成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			String msg = "sms send message test";
			Logger.comment("手机号:" + phone);
			spb.setMsg(msg);
			spb.setPhone(phone);
			spb.setPriority("1");
			spc.setData(spb);
			spc.callService();
			Logger.comment("SmsProxyCall send:" + spc.getEntityString());
			Logger.comment("SmsProxyCall return:" + spc.getReturnData());
			Thread.sleep(8000);
			List<SmsLog> sms = new SmsLogWapper().selectByPhone(phone);
			Logger.verifyEquals(true, sms.size() == 4, "验证发送短信");
			if (sms.size() > 0) {
				for (SmsLog s : sms) {
					Logger.debug(s.getAction());
					if (!s.getAction().equals("Received")) {
						Logger.verifyEquals(MessageSupplyEnum.WM梦网平台通知通道.toString(), s.getMessagesupply(), "验证发送的渠道");
						Logger.verifyEquals(true, s.getMessage().contains(msg), "验证短信内容");
					}
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	 @Test
	 @Category(P1.class)
	 @TestCase
	 @Ignore
	// 默认配置不加黑名单，需要修改smsproxy 站点配置 <add key="UseBlankPolicy" value="true"/>
	public void Tc_SmsProxy2_003() {
		Logger.start(true,"默认配置黑名单拦截，5次");
		try {
			String phone = TestDataManager.getPhoneNum();
			String msg = "sms send message test";
			Logger.comment("手机号:" + phone);
			for (int i = 0; i < 6; i++) {
				spb = new SmsProxyBean();
				spc = new SmsProxyCall();
				spb.setMsg(msg);
				spb.setPhone(phone);
				spc.setData(spb);
				spc.callService();
				Logger.comment("SmsProxyCall send:" + spc.getEntityString());
				Logger.comment("SmsProxyCall return:" + spc.getReturnData());
			}
			Logger.verifyEquals(true, spc.getReturnData().contains(ECode.SMS_BLOCK), "返回提示:" + ECode.SMS_BLOCK);
			Thread.sleep(8000);
			List<SmsLog> sms = new SmsLogWapper().selectByPhone(phone);
			// 5*4成功+1*2失败 SMS_BLOCK 只会记录2条
			Logger.verifyEquals(true, sms.size() == 22, "验证发送短信：" + sms.size());
			boolean InBlackList = false;
			for (SmsLog smsLog : sms) {
				if (smsLog.getAction().equals("InBlackList")) {
					InBlackList = true;
				}
			}
			Logger.verifyEquals(true, InBlackList, "包含结果InBlackList");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SmsProxy2_004() {
		Logger.start(true,"新建配置黑名单拦截，6次");
		try {
			String phone = TestDataManager.getPhoneNum();
			String msg = "sms send message test";
			Logger.comment("手机号:" + phone);
			for (int i = 0; i < 7; i++) {
				spb = new SmsProxyBean();
				spc = new SmsProxyCall();
				spb.setMsg(msg);
				spb.setPhone(phone);
				spb.setPriority("1");
				spc.setData(spb);
				spc.callService();
				Logger.comment("SmsProxyCall send:" + spc.getEntityString());
				Logger.comment("SmsProxyCall return:" + spc.getReturnData());
			}
			Logger.verifyEquals(true, spc.getReturnData().contains(ECode.SMS_BLOCK), "返回提示:" + ECode.SMS_BLOCK);
			Thread.sleep(8000);
			List<SmsLog> sms = new SmsLogWapper().selectByPhone(phone);
			// 6*4成功+1*2失败 SMS_BLOCK 只会记录2条
			Logger.verifyEquals(true, sms.size() == 26, "验证发送短信：" + sms.size());
			boolean InBlackList = false;
			for (SmsLog smsLog : sms) {
				if (smsLog.getAction().equals("InBlackList")) {
					InBlackList = true;
				}
			}
			Logger.verifyEquals(true, InBlackList, "包含结果InBlackList");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
